require 'extract/rules/xml_rule'
require 'extract/rules/xml_rule_multi'
require 'singleton'
require 'uri'
require 'enum'

module Extract
  
  class BloggerRules < Enum
    
    public
    
    class Occupation < XMLRule
      def xpath
        "/html/body/div[@id='body']/div/div[@id='main-wrap']/div[@id='main']/div[@id='m2']/div[@id='m3']/ul/li/span[@class='title']/a"
      end
      
      protected
      
      def handle_val(val, dest_receiver)
        dest_receiver.occupation = val
      end
    end
    
    class Role < XMLRule
      def xpath
        "/html/body/div[@id='body']/div/div[@id='main-wrap']/div[@id='main']/div[@id='m2']/div[@id='m3']/ul/li/span[@class='role']/a"
      end
      
      protected
      
      def handle_val(val, dest_receiver)
        dest_receiver.role = val
      end
    end
    
    class Country < XMLRule
      def xpath
        "/html/body/div[@id='body']/div[1]/div[@id='main-wrap']/div[@id='main']/div[@id='m2']/div[@id='m3']/ul/li/span[@class='country-name']/a"
      end
      
      protected
      
      def handle_val(val, dest_receiver)
        dest_receiver.country = val
      end
    end
    
    class LocaleCountry < XMLRule
      def xpath
        "/html/body/div[@id='body']/div[1]/div[@id='main-wrap']/div[@id='main']/div[@id='m2']/div[@id='m3']/ul/li/span[@class='country-name']/a/@href"
      end
      
      protected
      
      def preproces_val(val)
        ret = ""
        uri = URI.parse val
        uri.query.split(';').each do |substr| 
          pair = substr.split '='
          ret = pair[1] if pair[0].eql?("loc0")
        end
        
        val.replace ret
        super val
      end
      
      
      def handle_val(val, dest_receiver)
        dest_receiver.locale_country = val.eql?("") ? nil : val
      end
    end
    
    class Locality < XMLRule
      def xpath
        "/html/body/div[@id='body']/div[1]/div[@id='main-wrap']/div[@id='main']/div[@id='m2']/div[@id='m3']/ul/li/span[@class='locality']/a"
      end
      
      protected
      
      def handle_val(val, dest_receiver)
        dest_receiver.locality = val
      end
    end
    
    class State < XMLRule
      def xpath
        "/html/body/div[@id='body']/div[1]/div[@id='main-wrap']/div[@id='main']/div[@id='m2']/div[@id='m3']/ul/li/span[@class='region']/a"
      end
      
      protected
      
      def handle_val(val, dest_receiver)
        dest_receiver.state = val
      end
    end
    
    class FullName < XMLRule
      def xpath
        "/html/body/div[@id='body']/div[1]/div[@id='main-wrap']/div[@id='main']/div[@id='m2']/div[@id='m3']/h1"
      end
      
      protected
      
      def handle_val(val, dest_receiver)
        dest_receiver.full_name = val
      end
    end
    
    class FirstName < FullName
      
      protected
      
      def preproces_val(val)
        d = val.match /(\w+) (\w+)/
        val.replace d.nil? ? "" : d[1]
        super val
      end
      
      def handle_val(val, dest_receiver)
        dest_receiver.first_name = val.eql?("") ? nil :  val
      end
    end
    
    class LastName < FullName
      
      protected
      
      def preproces_val(val)
        d = val.match /(\w+) (\w+)/
        val.replace d.nil? ? "" : d[2]
        super val
      end
      
      def handle_val(val, dest_receiver)
        dest_receiver.last_name = val.eql?("") ? nil :  val
      end
    end
    
    class Gender < XMLRule
      def xpath
        "/html/body/div[@id='body']/div[1]/div[@id='main-wrap']/div[@id='main']/div[@id='m2']/div[@id='m3']/ul/li[./strong[text()='Sesso:']]"
      end
      
      protected
      
      def preproces_val(val)
        val.replace "male" if val.eql? "Maschio"
        val.replace "female" if val.eql? "Femmina"
      end
      
      def handle_val(val, dest_receiver)
        dest_receiver.gender = val
      end
    end
    
    class Email < XMLRule
      def xpath
        "/html/body/div[@id='body']/div/div[@id='sidebar']/div/div/div/ul/li/script/comment()"
      end
      
      protected
      
      def preproces_val(val)
        email = val.match(/printEmail\((.*),(.*)\)/)[1].to_s
        email.slice! 0
        email.chomp! ".biz\""
        val.replace email
      end
      
      def handle_val(val, dest_receiver)
        dest_receiver.email = val
      end
    end
    
    class Interests < XMLRuleMulti
      def xpath
        "/html/body/div[@id='body']/div/div[@id='main-wrap']/div[@id='main']/div[@id='m2']/div[@id='m3']/ul/li/a[contains(@href,'t=i')]"
      end
      
      protected
      
      def handle_val(val, dest_receiver)
        dest_receiver.interests = val
      end
    end
    
    class Music < XMLRuleMulti
      def xpath
         "/html/body/div[@id='body']/div/div[@id='main-wrap']/div[@id='main']/div[@id='m2']/div[@id='m3']/ul/li/a[contains(@href,'t=s')]"
      end
      
      protected
      
      def handle_val(val, dest_receiver)
        dest_receiver.music = val
      end
    end
    
    class Movie < XMLRuleMulti
      def xpath
         "/html/body/div[@id='body']/div/div[@id='main-wrap']/div[@id='main']/div[@id='m2']/div[@id='m3']/ul/li/a[contains(@href,'t=m')]"
      end
      
      protected
      
      def handle_val(val, dest_receiver)
        dest_receiver.movies = val
      end
    end
    
    class Book < XMLRuleMulti
      def xpath
         "/html/body/div[@id='body']/div/div[@id='main-wrap']/div[@id='main']/div[@id='m2']/div[@id='m3']/ul/li/a[contains(@href,'t=b')]"
      end
      
      protected
      
      def handle_val(val, dest_receiver)
        dest_receiver.books = val
      end
    end
    
    protected
    
    def initialize
      super()
      @collections[:Occupation] = Occupation.instance
      @collections[:Role] = Role.instance
      @collections[:LocaleCountry] = LocaleCountry.instance    
      @collections[:Country] = Country.instance
      @collections[:Locality] = Locality.instance
      @collections[:State] = State.instance
      @collections[:FullName] = FullName.instance
      @collections[:FirstName] = FirstName.instance
      @collections[:LastName] = LastName.instance
      @collections[:Gender] = Gender.instance
      @collections[:Email] = Email.instance
      @collections[:Interests] = Interests.instance
      @collections[:Music] = Music.instance
      @collections[:Movie] = Movie.instance
      @collections[:Book] = Book.instance
    end
    
  end
  
end